﻿// JavaScript Document
// itea 2009-08-07

$(document).ready(function(){
	$("#tabs").tabs().bind('tabsshow', function(event, ui) {
		if(ui.tab.hash == '#newsDetail')
			newsDetail.syncEdit();
		
		if(newsDetail.modified) {
			$('input[type=button][value=立即发布]').val('修改并立即发布');
			$('input[type=button][value=计划发布]').val('修改并计划发布');
		} else {
			$('input[type=button][value=修改并立即发布]').val('立即发布');
			$('input[type=button][value=修改并计划发布]').val('计划发布');
		}
	});
	
	$("#s-tabs").tabs();
});

var newsList = {
	ls: new Array(),
	newsIdx: {},
	feed_ls: new Array(),
	feed_newsIdx: {},
	refresh: function() {
		var that = this;
		$('#newsList input[type=button]').attr('disabled', true);
		newsAdminDwr.listAuditingNews(function(sresult) {
			that.newsIdx = {};
			that.ls = new Array();
			$('#newsList div').remove();
			for(var i=0; i<sresult.result.length; i++)
				that.add(sresult.result[i]);
			$('#newsList input[type=button]').attr('disabled', false);
		});
	},
	fetchFeed: function() {
		var url = $.trim($('#newsFeed input[name=feedurl]').val());
		var that = this;
		if(url) {
			$('#newsFeed input[type=button]').attr('disabled', true);
			feedsDwr.listFeedNews(url, function(sresult) {
				that.feed_newsIdx = {};
				that.feed_ls = new Array();
				$('#newsFeed div').remove();
				for(var i=0; i<sresult.result.length; i++)
					that.feed_add(sresult.result[i]);
				$('#newsFeed input[type=button]').attr('disabled', false);
			});
		}
	},
	fetchFeeds: function() {
		var that = this;
		feedsDwr.listFeeds(function(sresult){
			that.listFeeds(sresult.result);
		});
	},
	listFeeds: function(feedsIdx) {
		var html = '';
		var i = 0;
		this.feedsIdx = {};
		for(var url in feedsIdx) {
			this.feedsIdx[i] = url;
			html += '<div><a class="ui-state-default news-item" href="javascript:newsList.feed('+ i +')">'+ feedsIdx[url] +'</a> <span></span></div>';
			i++;
		}
		$('#feeds').children('div').remove().end().append(html);
	},
	feed: function(i) {
		$('#newsFeed input[name=feedurl]').val(this.feedsIdx[i] || '');
		$("#s-tabs").tabs('select', 1);
		this.fetchFeed();
	},
	add: function(n) {
		this.ls.push(n);
		this.newsIdx[n.id] = n;
		this.append(n);
	},
	append: function(n) {
		var html = '<div id="nl_'+ n.id +'"><a class="ui-state-default news-item" href="javascript:newsList.audit('+ n.id +')">'+ n.subject +'</a> <span></span></div>';
		$('#newsList').append(html);
	},
	feed_add: function(n) {
		this.feed_ls.push(n);
		this.feed_newsIdx[n.id] = n;
		this.feed_append(n);
	},
	feed_append: function(n) {
		var html = '<div id="nl_'+ n.id +'"><a class="ui-state-default news-item" href="javascript:newsList.audit('+ n.id +')">'+ n.subject +'</a> <span></span></div>';
		$('#newsFeed').append(html);
	},
	audit: function(id) {
		var n = this.newsIdx[id];
		if(n)
			n._s = 'posted';
		else {
			n = this.feed_newsIdx[id];
			if(n)
				n._s = 'feed';
		}
		if(n)
			newsDetail.show(n);
	},
	remove: function(id) {
		$('#nl_'+ id).remove();
		var n = this.newsIdx[id] || this.feed_newsIdx[id];
		switch(n._s) {
		case 'posted':
			delete this.newsIdx[id];
		case 'feed':
			delete this.feed_newsIdx[id];
		}
	}
};

var newsDetail = {
	news: null,
	show: function(n) {
		this.news = n;
		this.render(n);
		this.renderEdit(n);
	},
	render: function(ne) {
		var ne = ne || this.news;
		var html = '<div><input type="button" value="计划发布" onclick="newsDetail.schedule()"/> <input type="button" value="立即发布" onclick="newsDetail.publish()"/> <input type="button" value="删除" onclick="newsDetail.remove()"/></div>';
		html += '<h2 class="time">'+ ne.createTime +'</h2>';
		if(ne.subject)
			html += '<h1 class="subject">'+ ne.subject +'</h1>';
		if(ne.imageUrl)
			html += '<img src="'+ ne.imageUrl +'"/>';
		if(ne.content)
			html += ne.content;
		html += '<div class="bottom-info clear"><ul>';
		if(ne.srcLinkUrl)
			html += '<li><a target="_blank" href="'+ ne.srcLinkUrl +'">来源</a></li>';
		if(ne.poster) {
			html += '<li>投稿人: <@poster></li>'
				.replace(/<@poster>/, ne.posterLink ? '<a href="'+ ne.posterLink +'">'+ ne.poster +'</a>' : ne.poster);
		}
		html += '</ul><div class="clear"></div></div>';
		html += '<div><input type="button" value="计划发布" onclick="newsDetail.schedule()"/> <input type="button" value="立即发布" onclick="newsDetail.publish()"/> <input type="button" value="删除" onclick="newsDetail.remove()"/></div>';
		
		$('#newsDetail').html(html);
	},
	syncEdit: function() {
		if(this.news == null)
			return;
		
		var n = {};
		n.subject = $.trim($('input[name=subject]').val());
		n.imageUrl = $.trim($('input[name=imageUrl]').val());
		n.content = $.trim($('textarea[name=content]').val());
		n.srcLinkUrl = $.trim($('input[name=srcLinkUrl]').val());
		
		if(!n.content.match('^<[pP]>[\\s+\\S+]*</[pP]>$'))
			n.content = '<p>'+ n.content +'</p>';
		
		n.id = this.news.id;
		n.createTime = this.news.createTime;
		this.render(n);
		
		if(n.subject != this.news.subject)
			this.modified = true;
		else if(n.imageUrl != this.news.imageUrl)
			this.modified = true;
		else if(n.content != this.news.content)
			this.modified = true;
		else if(n.srcLinkUrl != this.news.srcLinkUrl)
			this.modified = true;
		else
			this.modified = false;
	},
	renderEdit: function(ne) {
		$('input[name=subject]').val(ne.subject || '');
		$('input[name=imageUrl]').val(ne.imageUrl || '');
		$('textarea[name=content]').val(ne.content || '');
		$('input[name=srcLinkUrl]').val(ne.srcLinkUrl || '');
		$('input[name=updateBut]').attr('disabled', false);
	},
	publish: function() {
		var n = this.news;
		$('#newsDetail').empty();
		if(this.modified) {
			n.subject = $.trim($('input[name=subject]').val());
			n.imageUrl = $.trim($('input[name=imageUrl]').val());
			n.content = $.trim($('textarea[name=content]').val());
			n.srcLinkUrl = $.trim($('input[name=srcLinkUrl]').val());
			
			if(!n.content.match('^<[pP]>[\\s+\\S+]*</[pP]>$'))
				n.content = '<p>'+ n.content +'</p>';
			
			switch(n._s) {
			case 'posted':
				delete n._s;
				newsAdminDwr.editAndPublishPosted(n);
				break;
			case 'feed':
				delete n._s;
				newsAdminDwr.publishNews(n);
				break;
			}
		} else {
			switch(n._s) {
			case 'posted':
				delete n._s;
				newsAdminDwr.publishPosted(n.id);
				break;
			case 'feed':
				delete n._s;
				newsAdminDwr.publishNews(n);
				break;
			}
		}
		newsList.remove(n.id);
		this.clearEdit();
	},
	schedule: function() {
		var n = this.news;
		$('#newsDetail').empty();
		if(this.modified) {
			n.subject = $.trim($('input[name=subject]').val());
			n.imageUrl = $.trim($('input[name=imageUrl]').val());
			n.content = $.trim($('textarea[name=content]').val());
			n.srcLinkUrl = $.trim($('input[name=srcLinkUrl]').val());
			
			if(!n.content.match('^<[pP]>[\\s+\\S+]*</[pP]>$'))
				n.content = '<p>'+ n.content +'</p>';
			
			switch(n._s) {
			case 'posted':
				delete n._s;
				newsAdminDwr.editAndSchedulePosted(n);
				break;
			case 'feed':
				delete n._s;
				newsAdminDwr.scheduleNews(n);
				break;
			}
		} else {
			switch(n._s) {
			case 'posted':
				delete n._s;
				newsAdminDwr.schedulePosted(n.id);
				break;
			case 'feed':
				delete n._s;
				newsAdminDwr.scheduleNews(n);
				break;
			}
		}
		newsList.remove(n.id);
		this.clearEdit();
	},
	remove: function() {
		$('#newsDetail').empty();
		var n = this.news;
		if(n._s == 'posted')
			newsAdminDwr.removePosted(id);
		this.clearEdit();
		newsList.remove(n.id);
	},
	clearEdit: function() {
		this.news = null;
		$('input[name=subject]').val('');
		$('input[name=imageUrl]').val('');
		$('textarea[name=content]').val('');
		$('input[name=srcLinkUrl]').val('');
		$('input[name=updateBut]').attr('disabled', true);
	}
}